<?php
/**
 *
 * @author 孙春雷
 *        
 */
class Service_Common {

	/**
	 * 调用的数据库
	 *
	 * @var unknown
	 */
	protected $_db = 'readonlyerp';

	/**
	 * 根据sku查出供应商和供应商产品的信息
	 *
	 * @param unknown $sku        	
	 * @return Ambigous <multitype:, multitype:unknown NULL >
	 */
	public function get_supplier_by_sku($sku)
	{
        //START---------------------------------------cwy--------------------------------------------------------------------
        $providers = Newerpclientapi::getSupplierskuInfo($sku);
//		$providers = DB::select( 'erp_provider.*', 'erp_providerproducts.*' )->from( 'erp_provider' )
//			->join( 'erp_providerproducts' )
//			->on( 'erp_provider.id', '=', 'erp_providerproducts.iproviderID' )
//			->where( 'erp_providerproducts.iproductID', 'LIKE', trim( $sku ) )
//			->where( 'erp_provider.state', '=', 2 )
//			->order_by( 'erp_providerproducts.price' )
//			->execute( $this->_db )
//			->as_array();
        //END---------------------------------------cwy--------------------------------------------------------------------
		return $providers;
	}

	public function get_supplier_by_sku_new($sku) {
        $sql = "SELECT ss.supplier_id as id,ss.supplier_name as name,ss.price,ss.delivery FROM vmi_supplier_skus ss 
                        WHERE ss.sku='".$sku."' AND ss.isavailable=1 AND ss.isavailable=1 ORDER BY ss.price";
	    $providers = DB::query(Database::SELECT,$sql)->execute('newerp')->as_array();
	    return $providers;
    }

	/**
	 * 根据sku和供应商找到对应唯一的供应商产品信息
	 * @param unknown $sku
	 * @param unknown $supplier_id
	 * @return NULL
	 */
	public function get_sku_by_supplier($sku, $supplier_id)
	{
//		$sku = DB::select()->from( 'erp_providerproducts' )
//			->where( 'iproviderID', '=', $supplier_id )
//			->where( 'iproductID', '=', trim( $sku ) )
//			->execute( $this->_db )
//			->current();
//		return $sku;
	}

	/**
	 * 求sku的当月期初值，然后求最近一次采购价的成本，然后求定价成本，最后选择0
	 * @param string $sku   产品sku
	 * @return string $price 成本价
	 */
	public function get_sku_price($sku)
	{
		$date = date('Y-m');
		$date1 =$date.'-01';
		$inventory = ORM::factory('inventory')->where('inventory_date','=',$date1)->where('sku','=',$sku)->find();
		if($inventory->id!='' && $inventory->quantity!=0){
				$price = $inventory->cost/$inventory->quantity;
		}else{
			$warehouseorders = ORM::factory('warehouseorder')->where('sku','=',$sku)->where('type','=','10')->order_by('warehouse_date','DESC')->limit('1')->find();
			if($warehouseorders->id!='' && $warehouseorders->warehouse_quantity!=0){
				$price = $warehouseorders->cost/$warehouseorders->warehouse_quantity;
			}else{
                //START---------------------------------------cwy--------------------------------------------------------------------
                $skus = Skusystemapi::getSku($sku);
                if($skus){
                    $prices = Skusystemapi::getSkuPrice($sku);
                    $price = $prices[0]['num'];
                }else{
                    $price = 0;
                }
//				$product_value_sql ="SELECT products_sku,products_values FROM erp_products_data
//				WHERE  products_sku='".$sku."' limit 1";
//				$pv = DB::query(Database::SELECT,$product_value_sql)->execute('readonlyerp')->as_array();
//				if(!empty($pv)){
//					$price = floatval($pv[0]['products_values']);
//
//				}else{
//					$price = 0;
//
//				}
                //END----------------------------------------cwy------------------------------------------------------------
			}

		}
			return $price;
	}

	/**
	 * 求sku的单价，金额，成本,税额
	 * @param string  $price  单价
	 * @param  string $num		数量
	 * @param  string $tax_way	计税方式
	 * @param  string $tax_rate 税率
	 * @return array $re 数组 
	 */
	public function get_sku_money_cost($price,$num,$tax_way,$tax_rate)
	{
				$re = array();
				if($tax_way==0){//不计税时，成本=金额；税额=0
				$re['single_price'] = $price;
				$re['money'] =$price*$num;
				$re['cost'] = $re['money'];
				$re['tax_price'] = 0;
			}else if($tax_way==-1){//加内税的计算成本和税额
				$re['single_price'] = $price;
				$re['money'] =$price*$num;
				$re['cost'] = ($price*$num)/(1+$tax_rate);
				$re['tax_price'] = $re['cost']*$tax_rate;			

			}else if($tax_way==1){//加外税时的计算成本和税额
				$re['single_price'] = $price;
				$re['money'] =($price*$num)*(1+$tax_rate);
				$re['cost'] = $price*$num;
				$re['tax_price'] = $re['cost']*$tax_rate;

			}
		return $re;
	}
}
